package com.ztz.mynewdemo.activity;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.ztz.mynewdemo.R;
import com.ztz.mynewdemo.utils.ActivityCollector;
import com.ztz.mynewdemo.utils.AppConfigUtils;


public class BaseActivity extends AppCompatActivity {
    ProgressDialog progressDialog;
    public static String TAG = "BaseActivity";
    private CancelReceiver receiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        TAG=getClass().getName();
        ActivityCollector.addActivity(this);
        IntentFilter intentFilter=new IntentFilter();
        intentFilter.addAction("com.example.broadcastreceiver");
        receiver=new CancelReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(receiver,intentFilter);
    }
    public void showDialog(String msg){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(this);
        }
        progressDialog.setMessage(msg);
        progressDialog.show();
    }
    public void showDialog(){
        if(progressDialog==null){
            progressDialog=new ProgressDialog(this);
        }
        progressDialog.setMessage("");
        progressDialog.show();
    }
    public void closeDialog(){
        if(progressDialog!=null||progressDialog.isShowing()){
            progressDialog.dismiss();
        }
    }
    //使用此方法布局中必须有id为 toolbar
    public void setToolbar(String title){
        Toolbar toolbar= (Toolbar) findViewById(R.id.toolbar);
        setToolbar(title,toolbar);
    }
    public void setToolbar(String title, Toolbar toolbar){
        if(toolbar!=null){
            setSupportActionBar(toolbar);
            toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle(title);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    class CancelReceiver extends BroadcastReceiver
    {
        @Override
        public void onReceive(final Context context, Intent intent) {
            //对AlertDialog来说，需要依赖一个View，而View是对应于Activity的。所以，一般传入应该为Acitivity.this，
            // 即Activity自身context。不然此处使用本地广播会报异常
            AlertDialog.Builder builder=new AlertDialog.Builder(BaseActivity.this);
            builder.setTitle("提示");
            builder.setMessage("你确定要注销账号吗?");
            builder.setCancelable(false);
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    ActivityCollector.finishAll();
                    Intent intent=new Intent(context,LoginActivity.class);
                    context.startActivity(intent);
                    AppConfigUtils.getInstance().setLoginStatu(BaseActivity.this,true);
                }
            });
            builder.show();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //检测其他类内存泄漏
//        RefWatcher refWatcher= App.getRefWatcher(this);
//        refWatcher.watch(this);
        ActivityCollector.removeActivity(this);
        if(receiver!=null)
        {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
            receiver=null;
        }
    }
}
